using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.ServiceModel;

namespace MSNSearch.Activities
{
    [Designer(typeof(ConsoleActivityDesigner), typeof(IDesigner))]
    [ToolboxBitmap(typeof(Search), "Resources.Custom Activity Background - Live Expo.jpg")]
    public partial class Search : Activity
	{
		public Search()
		{
			InitializeComponent();
		}

        public static DependencyProperty InvokeEvent = DependencyProperty.Register("Invoke", typeof(EventHandler), typeof(Search));

        [Description("This is the event that is fired when the activity is invoked.")]
        [Category("PreExecution")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public event EventHandler Invoke
        {
            add
            {
                base.AddHandler(Search.InvokeEvent, value);
            }
            remove
            {
                base.RemoveHandler(Search.InvokeEvent, value);
            }
        }

        public static DependencyProperty SearchResultFieldMaskProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SearchResultFieldMask", typeof(ResultFieldMask), typeof(Search));
        [Description("Determines how to mask the result fields.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public ResultFieldMask SearchResultFieldMask
        {
            get
            {
                return ((ResultFieldMask)(base.GetValue(Search.SearchResultFieldMaskProperty)));
            }
            set
            {

                base.SetValue(Search.SearchResultFieldMaskProperty, value);

            }
        }

        public static DependencyProperty SearchSourceTypeProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SearchSourceType", typeof(SourceType), typeof(Search));
        [Description("Determines the source to search.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public SourceType SearchSourceType
        {
            get
            {
                return ((SourceType)(base.GetValue(Search.SearchSourceTypeProperty)));
            }
            set
            {

                base.SetValue(Search.SearchSourceTypeProperty, value);

            }
        }


        public static DependencyProperty ApplicationIDProperty = System.Workflow.ComponentModel.DependencyProperty.Register("ApplicationID", typeof(string), typeof(Search));
        [Description("Determines the source to search.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ApplicationID
        {
            get
            {
                return ((string)(base.GetValue(Search.ApplicationIDProperty)));
            }
            set
            {

                base.SetValue(Search.ApplicationIDProperty, value);

            }
        }

        public static DependencyProperty QueryTextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("QueryText", typeof(string), typeof(Search));
        [Description("The text to search for.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string QueryText
        {
            get
            {
                return ((string)(base.GetValue(Search.QueryTextProperty)));
            }
            set
            {

                base.SetValue(Search.QueryTextProperty, value);

            }
        }

        public static DependencyProperty SafeSearchProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SafeSearch", typeof(SafeSearchOptions), typeof(Search));
        [Description("Determines the types of search results returned.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public SafeSearchOptions SafeSearch
        {
            get
            {
                return ((SafeSearchOptions)(base.GetValue(Search.SafeSearchProperty)));
            }
            set
            {

                base.SetValue(Search.SafeSearchProperty, value);

            }
        }


        public static DependencyProperty CultureInfoProperty = System.Workflow.ComponentModel.DependencyProperty.Register("CultureInfo", typeof(string), typeof(Search));
        [Description("The culture to use in the search request.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string CultureInfo
        {
            get
            {
                return ((string)(base.GetValue(Search.CultureInfoProperty)));
            }
            set
            {

                base.SetValue(Search.CultureInfoProperty, value);

            }
        }

        public static DependencyProperty CountProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Count", typeof(int), typeof(Search));
        [Description("Determines the types of search results returned.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int Count
        {
            get
            {
                return ((int)(base.GetValue(Search.CountProperty)));
            }
            set
            {

                base.SetValue(Search.CountProperty, value);

            }
        }

        public static DependencyProperty OffsetProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Offset", typeof(int), typeof(Search));
        [Description("Determines the types of search results returned.")]
        [Category("Miscellaneous")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int Offset
        {
            get
            {
                return ((int)(base.GetValue(Search.OffsetProperty)));
            }
            set
            {

                base.SetValue(Search.OffsetProperty, value);

            }
        }


        #region GeoCode Properties
        public static DependencyProperty LatitudeProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Latitude", typeof(double), typeof(Search));
        [Description("This is the latitude to check traffic for. ")]
        [Category("GeoCode")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public double Latitude
        {
            get
            {
                return ((double)(base.GetValue(Search.LatitudeProperty)));
            }
            set
            {
                base.SetValue(Search.LatitudeProperty, value);
            }
        }
        public static DependencyProperty LongitudeProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Longitude", typeof(double), typeof(Search));
        [Description("This is the longitude to check traffic for. ")]
        [Category("GeoCode")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public double Longitude
        {
            get
            {
                return ((double)(base.GetValue(Search.LongitudeProperty)));
            }
            set
            {
                base.SetValue(Search.LongitudeProperty, value);
            }
        }
        #endregion

        public static DependencyProperty RadiusProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Radius", typeof(double), typeof(Search));
        [Description("This is the radius to use with Location. ")]
        [Category("GeoCode")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public double Radius
        {
            get
            {
                return ((double)(base.GetValue(Search.RadiusProperty)));
            }
            set
            {
                base.SetValue(Search.RadiusProperty, value);
            }
        }


        public static DependencyProperty SearchFlagsProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SearchFlags", typeof(SearchFlags), typeof(Search));
        [Description("This is the radius to use with Location. ")]
        [Category("GeoCode")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public SearchFlags SearchFlags
        {
            get
            {
                return ((SearchFlags)(base.GetValue(Search.SearchFlagsProperty)));
            }
            set
            {
                base.SetValue(Search.SearchFlagsProperty, value);
            }
        }

        public static DependencyProperty ResultProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Result", typeof(SearchResponse), typeof(Search));
        [Description("The response from the search.")]
        [Category("Result")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public SearchResponse Result
        {
            get
            {
                return ((SearchResponse)(base.GetValue(Search.ResultProperty)));
            }
            set
            {
                base.SetValue(Search.ResultProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            base.RaiseEvent(Search.InvokeEvent, this, EventArgs.Empty);

            BasicHttpBinding b = new BasicHttpBinding();
            EndpointAddress ea = new EndpointAddress("http://soap.search.msn.com:80/webservices.asmx");

            ChannelFactory<IMSNSearch> cf = new ChannelFactory<IMSNSearch>(b, ea);
            IMSNSearch chn = cf.CreateChannel();



            int arraySize = 1;
            SourceRequest[] sourceRequest = new SourceRequest[arraySize];
            sourceRequest[0] = new SourceRequest();
            sourceRequest[0].Source = SearchSourceType;
            sourceRequest[0].ResultFields = SearchResultFieldMask;
            sourceRequest[0].Count = Count;
            sourceRequest[0].Offset = Offset;
            
            SearchRequest searchRequest = new SearchRequest();
            

            searchRequest.AppID = ApplicationID;
            searchRequest.SafeSearch = SafeSearch;
            searchRequest.CultureInfo = "en-US";
            Location location = new Location();
            if ((Latitude != 0.00) && (Longitude !=0))
             {
                location.Latitude = Latitude;
                location.Longitude = Longitude;
                location.Radius = Radius;
            }
                searchRequest.Location = location;
   
            searchRequest.Requests = sourceRequest;
            searchRequest.Query = QueryText;
            searchRequest.Flags = SearchFlags;
            SearchResponse searchResponse;
            searchResponse = chn.Search(searchRequest);



            Result = chn.Search(searchRequest);

            
            return base.Execute(executionContext);

        }
    }
}
